/**
 * 
 */
package com.oet.webservice.business;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.jws.WebService;
import javax.mail.util.ByteArrayDataSource;

import org.apache.cxf.aegis.type.java5.IgnoreProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.oet.persistence.entity.UploadedFile;
import com.oet.persistence.entity.MngrAccount;
import com.oet.persistence.entity.MngrRole;
import com.oet.persistence.manager.OetDbMngr;
import com.oet.service.manager.BackupManager;
import com.oet.webservice.basic.OperationStatus;
import com.oet.webservice.basic.WebServiceListParams;

/**
 * @author y140zhan
 *
 */
@WebService(endpointInterface = "com.oet.webservice.business.AccountMngrService")
@Transactional
public final class AccountMngrServiceImpl implements AccountMngrService {
	
	protected final OetDbMngr oetDbMngr;
	
	@Autowired
	public AccountMngrServiceImpl(OetDbMngr accountDbMngr) {
		this.oetDbMngr = accountDbMngr;
	}



	public WebMngrAccountListResult findMngrAccounts(String name, WebServiceListParams listRarams) {
		// TODO Auto-generated method stub
		List<MngrAccount> list = (List<MngrAccount>) oetDbMngr.findAllYdkAccounts(name);
//		for (YdkAccount a : list)
//		{
//			a.getYdkRole().getMask();
//			a.getYdkRole().setYdkAccounts(null);
//		}
		return getSpecifiedPageList(list , listRarams, new WebMngrAccountListResult());
	}
	

	public WebMngrAccountListResult getMngrAccounts(WebServiceListParams listRarams) {
		// TODO Auto-generated method stub
		List<MngrAccount> accountList = (List<MngrAccount>) oetDbMngr.getAllYdkAccounts();
//		for (YdkAccount a : accountList)
//		{
//			a.getYdkRole().getMask();
//			a.getYdkRole().setYdkAccounts(null);
//		}
		
		return getSpecifiedPageList(accountList , listRarams, new WebMngrAccountListResult());
	}
	
	public WebMngrRoleListResult getMngrRoles(WebServiceListParams listRarams) {
		// TODO Auto-generated method stub
		
		List<MngrRole> list = (List<MngrRole>)(oetDbMngr.getAllYdkRoles());
		//Lazy.get;
//		for(YdkRole e: list)
//		{
//			e.setYdkAccounts(null);;
//		}
		return getSpecifiedPageList(list , listRarams, new WebMngrRoleListResult());
	}
	
	public WebMngrRoleListResult getAllMngrRoles() {
		// TODO Auto-generated method stub
		WebMngrRoleListResult result = new WebMngrRoleListResult();
		result.setMngrRoles((List) new ArrayList(oetDbMngr.getAllYdkRoles()));
		
		//Lazy.get;
//		for(YdkRole e: result.getYdkRoles())
//		{
//			e.setYdkAccounts(null);
//		}
		result.setCurrentPage(1);
		result.setTotalItems(result.getMngrRoles().size());
		result.setTotalPages(1);
		return result;
	}
	
	public WebMngrRoleListResult getAllMngrRolesEager() {
		// TODO Auto-generated method stub
		WebMngrRoleListResult result = new WebMngrRoleListResult();
		result.setMngrRoles((List) new ArrayList(oetDbMngr.getAllYdkRoles()));
		
		//Lazy.get;
//		for(YdkRole e: result.getYdkRoles())
//		{
//			e.getYdkAccounts().size();
//		}
		result.setCurrentPage(1);
		result.setTotalItems(result.getMngrRoles().size());
		result.setTotalPages(1);
		return result;
	}
	
	
	public WebMngrAccountListResult getAllMngrAccounts() {
		// TODO Auto-generated method stub
		WebMngrAccountListResult result = new WebMngrAccountListResult();
		
		result.setMngrAccounts((List) oetDbMngr.getAllYdkAccounts());
		result.setCurrentPage(1);
		result.setTotalItems(result.getMngrAccounts().size());
		result.setTotalPages(1);
//		for (YdkAccount a : result.getYdkAccounts())
//		{
//			a.getYdkRole().getMask();
//			a.getYdkRole().setYdkAccounts(null);
//		}
		return result;
	}
	
	
	
	public WebServiceMngrAccount getMngrAccountById(Long id) {
		// TODO Auto-generated method stub
		WebServiceMngrAccount result = new WebServiceMngrAccount();
		if (id == null)
		{
			result.getOs().setIsSuccessful(false);
			result.getOs().setFailureReason("Null input Id");
			return result;
		}
		MngrAccount instance = oetDbMngr.getYdkAccount(id);
		if (instance == null)
		{
			result.getOs().setIsSuccessful(false);
			result.getOs().setFailureReason("YdkAccount not found by id: " + id);
			return result;
		}
		
		result.setMngrAccount(instance);
		//Eager get
//		instance.getYdkRole().getMask();
//		instance.getYdkRole().setYdkAccounts(null);
		return result;
	}
	


	public WebServiceMngrAccount getMngrAccountByName(String name) {
		// TODO Auto-generated method stub
		WebServiceMngrAccount result = new WebServiceMngrAccount();
		if (name == null)
		{
			result.getOs().setIsSuccessful(false);
			result.getOs().setFailureReason("Null input Id");
			return result;
		}
		MngrAccount instance = oetDbMngr.getYdkAccountByName(name);
		if (instance == null)
		{
			result.getOs().setIsSuccessful(false);
			result.getOs().setFailureReason("YdkAccount not found by username: " + name);
			return result;
		}
		
		//Eager get
//		instance.getYdkRole().getMask();
//		instance.getYdkRole().setYdkAccounts(null);
		
		result.setMngrAccount(instance);
		return result;
	}







	public OperationStatus storeMngrAccount(WebServiceMngrAccount instance) {
		OperationStatus os = new OperationStatus();
		if (instance == null || instance.getMngrAccount() == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		//TODO some logic book
		oetDbMngr.storeYdkAccount(instance.getMngrAccount());
		return os;
	}

	public OperationStatus updateMngrAccount(WebServiceMngrAccount instance) {
		OperationStatus os = new OperationStatus();
		if (instance == null || instance.getMngrAccount() == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		//TODO some logic book
		oetDbMngr.updateYdkAccount(instance.getMngrAccount());
		return os;
	}
	
	public OperationStatus assignMngrRole(Long ydkRoleId, Long id) {
		// TODO Auto-generated method stub
		OperationStatus os = new OperationStatus();
		if (id == null || ydkRoleId == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		MngrAccount ydkAccount = oetDbMngr.getYdkAccount(id);
		if (ydkAccount == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Could not find YdkAccount by id : " + id);
			return os;
		}
		
		MngrRole ydkRole = oetDbMngr.getYdkRole(id);
		if (ydkRole == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Could not find YdkRole by id : " + ydkRoleId);
			return os;
		}

		ydkAccount.addRole(ydkRole.getMask());
		oetDbMngr.updateYdkAccount(ydkAccount);
		return os;
	}

	public OperationStatus deactiveMngrAccount(Long id) {
		// TODO Auto-generated method stub
		OperationStatus os = new OperationStatus();
		MngrAccount ydkAccount = oetDbMngr.getYdkAccount(id);
		
		if (ydkAccount == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Could not find YdkAccount by id : " + id);
			return os;
		}
		
		ydkAccount.setIsActived(false);
		oetDbMngr.updateYdkAccount(ydkAccount);
		return os;

	}

	public OperationStatus deleteMngrAccount(Long id) {
		// TODO Auto-generated method stub
		OperationStatus os = new OperationStatus();
		oetDbMngr.deleteYdkAccount(id);
		return os;
	}
	


	public OperationStatus storeMngrRole(WebServiceMngrRole instance) {
		OperationStatus os = new OperationStatus();
		if (instance == null || instance.getMngrRole() == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		//TODO some logic book
		oetDbMngr.storeYdkRole(instance.getMngrRole());
		return os;
	}


	public OperationStatus updateMngrRole(WebServiceMngrRole instance) {
		OperationStatus os = new OperationStatus();
		if (instance == null || instance.getMngrRole() == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		//TODO some logic book
		oetDbMngr.updateYdkRole(instance.getMngrRole());
		return os;
	}
	
	
	public OperationStatus deleteMngrRole(Long id) {
		OperationStatus os = new OperationStatus();
		oetDbMngr.deleteYdkRole(id);
		return os;
	}

	
	
	
	public OperationStatus renewMngrAccountKeyPair(Long id) {
		
		OperationStatus os = new OperationStatus();
		
		// TODO Auto-generated method stub
		
		return os;
		
	}
	
	
	public OperationStatus systemKeyPairBackup() {
		OperationStatus os = new OperationStatus();
		
		// TODO Auto-generated method stub
		
		return os;
		
	}
	
	

	public OperationStatus systemKeyPairRenew() {
		OperationStatus os = new OperationStatus();
		
		// TODO Auto-generated method stub
		
		return os;
		
	}

	public String backupDb() {
//		OperationStatus os = new OperationStatus();
		
		// TODO Auto-generated method stub
		String filename = BackupManager.backupDb();
		
		return filename;
		
	}
	
//	@IgnoreProperty
	public OperationStatus backupResources() {
		OperationStatus os = new OperationStatus();
		
		// TODO Auto-generated method stub
		
		return os;
		
	}
	
	
	private WebMngrAccountListResult getSpecifiedPageList(List list, WebServiceListParams listParams,
			WebMngrAccountListResult result)
	{
		int maxPageSize = list.size() < listParams.getPageSize()
		? list.size() : listParams.getPageSize();

		int totalPage = (list.size()%listParams.getPageSize() == 0
			? list.size()/maxPageSize
			: list.size()/maxPageSize + 1);
		int currentPage = totalPage < listParams.getPageOffset() 
		? totalPage : listParams.getPageOffset();
		result.setTotalItems(list.size());
		result.setTotalPages(totalPage);
		result.setCurrentPage(currentPage);
		result.setMngrAccounts(
				list.subList(
				listParams.getPageSize()* currentPage,
				listParams.getPageSize()* (1 + currentPage)
				)
				);
		return result;
	}
	
	
	private WebMngrRoleListResult getSpecifiedPageList(List list, WebServiceListParams listParams,
			WebMngrRoleListResult result)
	{
		int maxPageSize = list.size() < listParams.getPageSize()
		? list.size() : listParams.getPageSize();

		int totalPage = (list.size()%listParams.getPageSize() == 0
			? list.size()/maxPageSize
			: list.size()/maxPageSize + 1);
		int currentPage = totalPage < listParams.getPageOffset() 
		? totalPage : listParams.getPageOffset();
		result.setTotalItems(list.size());
		result.setTotalPages(totalPage);
		result.setCurrentPage(currentPage);
		result.setMngrRoles(
				list.subList(
				listParams.getPageSize()* currentPage,
				listParams.getPageSize()* (1 + currentPage)
				)
				);
		return result;
	}



	public OperationStatus restoreDb(UploadedFile restoreDbFile) {
		// TODO Auto-generated method stub
		OperationStatus os = new OperationStatus();
		if (restoreDbFile != null && restoreDbFile.getFileContent().length > 0)
		{
			BackupManager.restoreDb(restoreDbFile.getFileContent());
		}
		return os;
	}






}
